package com.jy.adscm.operation.cg.controller;


import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.CgOrder;
import com.jy.adscm.operation.cg.bean.CgPayOrder;
import com.jy.adscm.operation.cg.bean.CgTask;
import com.jy.adscm.operation.cg.bean.QueryCgTask;
import com.jy.adscm.operation.cg.service.CgTaskService;
import com.jy.adscm.operation.mail.bean.MailBean;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.utils.AdscmDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 采购任务 前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-07-07
 */
@RestController
@RequestMapping("adscm/operation/cg/cgTask")
public class CgTaskController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    CgTaskService cgTaskService;
    @Value("${spring.mail.username}")
    String email;
    /**
     * 增加bom信息
     * @param cgTask
     * @param request
     * @return
     */
    @AdscmLogger("增加采购任务信息")
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody CgTask cgTask, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if(!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw  new AdscmSystemException("token 是空的");
        }
        //查询最大的

        cgTask.setCreator(token.getUser().getId().toString());
        cgTask.setCreateTime(new Date());
        cgTask.setIsArrive(0);
        cgTask.setIsCheckUp(0);
        cgTask.setIsDeliver(0);
        cgTask.setIsPay(0);
        cgTask.setIsReceiveInvoice(0);
        cgTask.setIsStart(0);
        cgTask.setIsStockUp(0);
        cgTask.setState(0);
        boolean insert = cgTaskService.insert(cgTask);
        return  new SimpleCoreResponse("200","新增成功",insert);
    }
    /**
     * 增加bom信息
     * @param cgTask
     * @param request
     * @return
     */
    @AdscmLogger("增加采购任务信息")
    @RequestMapping("insertsh")
    public SimpleCoreResponse insertsh(@RequestBody CgTask cgTask, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if(!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw  new AdscmSystemException("token 是空的");
        }
        //查询最大的
        Wrapper<CgTask> cgTaskWrapper =new EntityWrapper<>();
        String date = new SimpleDateFormat("yyMM").format(new Date());
        cgTaskWrapper.like("make_no","sh"+date, SqlLike.RIGHT);
        cgTaskWrapper.orderBy("make_no",false);
        cgTaskWrapper.last("limit 1");
        String makeNo="sh"+date+"00";
        List<CgTask> cgTasks = cgTaskService.selectList(cgTaskWrapper);
        if (cgTasks!=null&& cgTasks.size()>0){
            CgTask cgTask1 = cgTasks.get(0);
            makeNo= "sh"+String.valueOf(Integer.valueOf(cgTask1.getMakeNo().replace("sh",""))+1) ;
        }
        cgTask.setCreator(token.getUser().getId().toString());
        cgTask.setCreateTime(new Date());
        cgTask.setIsArrive(0);
        cgTask.setIsCheckUp(0);
        cgTask.setIsDeliver(0);
        cgTask.setIsPay(0);
        cgTask.setIsReceiveInvoice(0);
        cgTask.setIsStart(0);
        cgTask.setIsStockUp(0);
        cgTask.setState(0);
        cgTask.setMakeNo(makeNo);
        boolean insert = cgTaskService.insert(cgTask);
        return  new SimpleCoreResponse("200","新增成功",insert);
    }
    /**
     * 修改bom信息
     * @param cgTask
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @AdscmLogger("修改bom信息")
    @RequestMapping("update")
    public  SimpleCoreResponse  update(@RequestBody CgTask cgTask, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        cgTask.setCreator(token.getUser().getId().toString());
        cgTask.setCreateTime(new Date());
        boolean result = cgTaskService.updateCgTask(cgTask,token);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("select")
    public  SimpleCoreResponse  select(@RequestBody QueryCgTask queryCgTask, HttpServletRequest request) throws AdscmSystemException {
        Page<CgTask> page=new Page<>(queryCgTask.getCurrentPage(),queryCgTask.getPageSize());
        page= cgTaskService.selectCgTask(page,queryCgTask);
        return  new SimpleCoreResponse("200","查询成功",page);
    }

    /**
     * 查询总共需要多少钱
     * @param queryCgTask
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectTotals")
    public  SimpleCoreResponse  selectTotals(@RequestBody QueryCgTask queryCgTask, HttpServletRequest request) throws AdscmSystemException {
         Float result=  cgTaskService.selectTotals(queryCgTask);
        return  new SimpleCoreResponse("200","查询成功",result);
    }



    /**
     * 查询订单
     * @param id
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectCgOrderBy/{id}")
    public  SimpleCoreResponse  selectCgOrderBy(@PathVariable Integer id, HttpServletRequest request) throws AdscmSystemException {
        CgOrder cgOrder = cgTaskService.selectCgOrderBy(id);
        return  new SimpleCoreResponse("200","查询成功",cgOrder);
    }

    /**
     * 查询付款单
     * @param id
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectCgPayOrderBy/{id}")
    public  SimpleCoreResponse  selectCgPayOrderBy(@PathVariable Integer id, HttpServletRequest request) throws AdscmSystemException {
        CgPayOrder cgOrder = cgTaskService.selectCgPayOrderBy(id);
        return  new SimpleCoreResponse("200","查询成功",cgOrder);
    }

    /**
     * 导出数据时候 启动任务
     * @param id
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("startCgTask/{id}")
    public  SimpleCoreResponse  startCgTask(@PathVariable Integer id, HttpServletRequest request) throws AdscmSystemException {
        Boolean b= cgTaskService.startCgTask(id);
        return  new SimpleCoreResponse("200","启动成功",b);
    }
    @RequestMapping("sendEmail")
    public  SimpleCoreResponse  sendEmail(@RequestBody MailBean mailBean, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw new AdscmSystemException("未登录");
        }
        if (!email.equals(token.getUser().getQqNo())){
            throw new AdscmSystemException("您不能发送email请联系管理员");
        }
        cgTaskService.sendEmail(mailBean);
        return  new SimpleCoreResponse("200","发送成功","");
    }


}

